const models = require('../models/')
const moment = require('moment')

class StudentController {
  constructor() {}
  async findAll(whereObj, columnsArr, date) {
    var result = {}
    if (whereObj.id || whereObj.cookie || whereObj.class || whereObj.department) {
      result.where = whereObj
    }
    if (columnsArr.length) {
      result.attributes = columnsArr
    }
    // 今天的九点
    const compareTime = moment(moment().format('YYYY-MM-DD')).add(21,'hours')
    // 每个date显示的都是是从前一天的晚上九点到第二天晚上九点，如果date是今天，那么判断是否在晚上九点之前
    let ltTime = moment(date).add(21, 'hours') //big
    let gtTime = moment(date).subtract(3, 'hours') //small
    if(date===moment().format('YYYY-MM-DD')){
      // date 是今天
      if(moment()>compareTime){
        // 超过当天九点，
        ltTime = moment(date).add(24, 'hours') //big
        gtTime = moment(date).subtract(21, 'hours') //small
      }
    }
    result.include = [
      {
        model: models.prize,
        as: 'prizes'
      },
      {
        model: models.signinrecord,
        as: 'signinrecords',
        where: {
          time: {
            $lt: ltTime,
            $gt: gtTime
          }
        },
        required: false
      }
    ]
    const students = await models.student.findAll(result)
    return students
  }
  async update(id, updateObj) {
    var id = id
    var result = await models.student.update(updateObj, {
      where: {
        id: id
      }
    })
    if (result) {
      return true
    }
    return false
  }
}

module.exports = new StudentController()
